package com.zzh.socketlibrary.manager;

import android.text.TextUtils;
import android.util.Log;

/**
 * @author ：zuhui.zhang
 * time ： 2021/6/8 13:58
 * 带连接消息反馈的管理器类
 */
public class MessageSocketManager extends SocketManager implements ISocketManager.Host {
    private final String TAG = MessageSocketManager.class.getSimpleName();
    ISocketManager.Host host;

    public MessageSocketManager() {
        super.setHost(this);
    }

    public void setHost(ISocketManager.Host host) {
        this.host = host;
    }

    @Override
    public void onReceiveData(byte[] data) {
        Log.i(TAG, "onReceiveData");
        try {
            if (data == null || data.length < 1)
                return;
            // 消息数据
            if (data[data.length - 1] == SocketDataConst.MSG_TYPE_LAST_BYTE) {
                // 最后一个字节不需要，是信息类型
                String dataStr = new String(data, 0, data.length - 1, SocketManager.sCharset);
                if (TextUtils.isEmpty(dataStr))
                    return;
                if (!dataStr.contains(SocketDataConst.MSG_KEY_VALUE_SPLIT_STRING)) {

                    return;
                }
                String[] splits = dataStr.split(SocketDataConst.MSG_KEY_VALUE_SPLIT_STRING);
                if (splits.length < 2)
                    return;
                String key = splits[0];
                String value = splits[1];

                switch (key) {
                    case SocketDataConst.MSG_SOCKET_CONNECT:
                        //连接
                        if (SocketDataConst.MSG_SUCCESS.equals(value)) {
                            sendConnectMsg(SocketDataConst.MSG_SUCCESS);
                        } else {
                            // 断开连接
                            sendDisConnectMsg(SocketDataConst.MSG_SUCCESS);
                            Thread.sleep(500);
                            disconnect();
                            // 调用connect()等待pc重连
                        }
                        connect();
                }
            } else if (host != null) {
                // 普通数据
                host.onReceiveData(data);
            }
        } catch (Exception e) {
            Log.w(TAG, "onReceiveData e=" + e.getMessage());
        }
    }

    @Override
    public void onConnectSuccess() {
        Log.i(TAG, "onConnectSuccess");
        sendConnectMsg(SocketDataConst.MSG_SUCCESS);
        if (host != null)
            host.onConnectSuccess();
    }

    public void sendConnectMsg(String connState) {
        try {
            String connectedMsg = SocketDataConst.MSG_SOCKET_CONNECT_STATE
                    + SocketDataConst.MSG_KEY_VALUE_SPLIT_STRING + connState;
            byte[] rawData = connectedMsg.getBytes(sCharset);
            byte[] sendData = new byte[rawData.length + 1];
            sendData[sendData.length - 1] = SocketDataConst.MSG_TYPE_LAST_BYTE;
            System.arraycopy(rawData, 0, sendData, 0, rawData.length);
            sendData(sendData);
        } catch (Exception e) {
            Log.e(TAG, "sendConnectMsg e=" + e.getMessage());
        }
    }

    public void sendDisConnectMsg(String disConnState) {
        try {
            String connectedMsg = SocketDataConst.MSG_SOCKET_DISCONNECT_STATE
                    + SocketDataConst.MSG_KEY_VALUE_SPLIT_STRING + disConnState;
            byte[] rawData = connectedMsg.getBytes(sCharset);
            byte[] sendData = new byte[rawData.length + 1];
            sendData[sendData.length - 1] = SocketDataConst.MSG_TYPE_LAST_BYTE;
            System.arraycopy(rawData, 0, sendData, 0, rawData.length);
            sendData(sendData);
        } catch (Exception e) {
            Log.e(TAG, "sendDisConnectMsg e=" + e.getMessage());
        }
    }
}
